package com.ylxn;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylxn.mapper.IOrderMapper;
import com.ylxn.mapper.IUserMapper;
import com.ylxn.mapper.UserMapper;
import com.ylxn.pojo.Order;
import com.ylxn.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {
    @Test
    public void  test() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = build.openSession();
        IOrderMapper mapper = sqlSession.getMapper(IOrderMapper.class);
        List<Order> orders = mapper.findOrderAndUser();
        for (int i = 0; i < orders.size(); i++) {
            System.out.println(orders.get(i));
        }
    }

    @Test
    public void  test1() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = build.openSession();
        IUserMapper mapper = sqlSession.getMapper(IUserMapper.class);
        List<User> all = mapper.findAll();
        for (int i = 0; i < all.size(); i++) {
            System.out.println(all.get(i));
        }
    }

    @Test
    public void  test2() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = build.openSession();
        IUserMapper mapper = sqlSession.getMapper(IUserMapper.class);
        List<User> all = mapper.findAllUserAndRole();
        for (int i = 0; i < all.size(); i++) {
            System.out.println(all.get(i));
        }
    }

    private IUserMapper iUserMapper;
    @Before
    public void beforeInit() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = build.openSession(true);
        iUserMapper = sqlSession.getMapper(IUserMapper.class);
    }

    @Test
    public void test5(){
        List<User> users = iUserMapper.selectUsers();
        for (int i = 0; i < users.size(); i++) {
            System.out.println(users.get(i));
        }
    }

    @Test
    public void test6(){
        List<User> allUser = iUserMapper.findAllUserAndRoleBy();
        for (User user : allUser) {
            System.out.println(user);
        }
    }

    @Test
    public void test7(){
        PageHelper.startPage(1, 1);
        List<User> allUser = iUserMapper.findAll();
        for (User user : allUser) {
            System.out.println(user);
        }
        PageInfo<User> pageInfo = new PageInfo<>(allUser);
        int endRow = pageInfo.getEndRow();
        long total = pageInfo.getTotal();
        System.out.println(endRow + "end" + total + "total");
    }
    @Test
    public void test8() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = build.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = new User();
        user1.setId(1);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id",1);
        List<User> users = mapper.selectByExample(example);
        System.out.println(users);

    }

    @Test
    public void test9(){
    }

}
